package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Update extends JInternalFrame {
	Connection conn;// DatabaseTest에서 받아온 커넥션
    DataBaseTest main;
	private JTextField code;
	private JTextField ea;
	private JLabel label;
	private JLabel label_1;
	private JButton search;
	private JLabel result;
	private JButton modi;
	private JLabel lblNewLabel;
	private JTextField serial;
	private JButton del;

	public Update(Connection conn,DataBaseTest d) {
		super("수정", true, true, true, true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0) {
				serial.setText("");
				code.setText("");
				ea.setText("");
			}
		});
		this.conn = conn;
		this.main=d;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getSearch());
		getContentPane().add(getResult());
		getContentPane().add(getModi());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerial());
		getContentPane().add(getDel());

	}
    public void setData(String s, String c, String e){
    	serial.setText(s);
    	code.setText(c);
    	ea.setText(e);
    	
    }
	

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(111, 106, 116, 21);
		}
		return code;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(111, 149, 116, 21);
		}
		return ea;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setBounds(42, 109, 57, 15);
		}
		return label;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setBounds(42, 152, 57, 15);
		}
		return label_1;
	}

	private JButton getSearch() {
		if (search == null) {
			search = new JButton("조회");
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {

						int r = Integer.parseInt(serial.getText()); // serial 값
																	// 가져오기!

						String sql = "select  * from protbl where serial=?";// 코드의
																			// 행을
																			// 찾는다.
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, r); // 첫번째 물음표에 serial 값

						ResultSet rs = pstmt.executeQuery();// select 된 결과를 받아서
															// ResultSet 타입으로
															// 받아서 rs 저장

						if (rs.next()) { // 값이 있는지 찾는다.

							code.setText(rs.getString("code"));// 찾은 값의 1번째를 코드
																// 텍스트 필드에 넣는다.
							ea.setText(rs.getInt("ea") + ""); // 찾은 값의 2번째인 수량을
																// 텍스트 필드에 넣는다.

							result.setText("serial " + serial.getText()
									+ "인 데이터가 조회되었습니다.");

						} else {
							code.setText("");
							ea.setText("");
							result.setText("조회된 데이터가 없습니다.");
						}
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			search.setBounds(278, 64, 116, 23);
		}
		return search;
	}

	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("New label");
			result.setBounds(12, 246, 410, 15);
		}
		return result;
	}

	private JButton getModi() {
		if (modi == null) {
			modi = new JButton("수정");
			modi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						int s = Integer.parseInt(serial.getText()); // 시리얼 값
																	// 가져오기
						String c = code.getText(); // 코드 값 가져오기
						int e = Integer.parseInt(ea.getText()); // 수량 값 가져오기

						String sql = "update protbl set code=?, ea=? where serial=?";

						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c); // 첫번째 물음표에 c 값을 ~
						pstmt.setInt(2, e); // 두번째 물음표에 e 값을 ~
						pstmt.setInt(3, s); // 세번째 물음표에 s 값을 ~

						pstmt.executeUpdate();// 삽입, 수정, 삭제시 사용
						pstmt.close();

						result.setText("데이터가 수정되었습니다.");
						
						if(main.select !=null) main.select.search();
					} catch (Exception ex) {

					}

				}
			});
			modi.setBounds(111, 196, 116, 23);
		}
		return modi;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(42, 68, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(111, 65, 116, 21);
		}
		return serial;
	}
	private JButton getDel() {
		if (del == null) {
			del = new JButton("삭제");
			del.setBounds(278, 196, 116, 23);
		}
		return del;
	}
}
